package com.pocket.demo.core.redis;

import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * 【demo-redis-1.3】
 *
 * @author cookie.
 **/
@Configuration
public class RedisConfig {

    /**
     * <P> redis序列化的工具定置类，下面这个请一定开启配置 <br>
     *   127.0.0.1:6379> keys <br>
     *   “ord:102” 序列化过 <br>
     *   “\xaclxedlxeelx05tixeelaord:102” 没有序列化过 <br>
     */
    @Bean
    public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {
        RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(lettuceConnectionFactory);
        // 设置key序列化方式string
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        // 设置value的序列化方式json，使用 GenericJackson2JsonRedisSerializer 替换默认序列化
        //redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        // 【demo-redis-1.4】 TODO  异常原因分析
        redisTemplate.setValueSerializer(new GenericFastJsonRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(new GenericFastJsonRedisSerializer());
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }
}
